﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Agency_PriceDEC : System.Web.UI.Page
{
    int projectid;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            projectid = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["projectid"]));
            string _strPassWord = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["PassWord"]));
            //if (!HZ.MBSM.BLL.Login.CheckPerson("jf001", _strPassWord, "4"))
            //{
            //    Response.Write("<script>window.alert('对不起,密码不对！');</script>");
            //    Response.Write("<script>window.location=\"systemlogo.aspx?Projectid=" + projectid.ToString() + "&Id=1\"</script>");
            //}
            //else
            //{
            Db db = new Db();
            //if (Config.EnableEncrypt)
            //{
            //    try
            //    {

            //        // 开始解密
            //        string sql_2 = string.Format("SELECT BIDID,EncryptedPrice,Factor,pricepassword FROM dbo.bidprice WHERE Projectid = {0} AND EncryptedPrice IS NOT NULL", projectid.ToString());
            //        System.Text.StringBuilder sql_3 = new System.Text.StringBuilder();
            //        DataTable tb = db.DataTable(sql_2);
            //        int count = 0;
            //        foreach (DataRow item in tb.Rows)
            //        {

            //            string SPPrice = item["EncryptedPrice"].ToString();
            //            SPPrice = HZ.Encrypt.EncryptWrapper.DecrytPrice(item["pricepassword"].ToString(), SPPrice);//获取解密报价
                        
            //            // SPPrice = HZ.Encrypt.EncryptWrapper.DecrytPrice(SPPrice);
            //            string bidid = item["bidid"].ToString();

            //            int factor;

            //            if (int.TryParse(item["Factor"].ToString(), out factor))
            //            {

            //            }
            //            else
            //            {
            //                factor = 1;
            //            }

            //            float smprice = float.Parse(SPPrice) / factor;


            //            sql_3.AppendFormat("UPDATE Project_Bid SET SPPrice = {0},smprice = {1} WHERE bidid = {2};", SPPrice, smprice, bidid);
            //            count++;
            //            if (count == 500)
            //            {

            //                db.Command(sql_3.ToString(), 360);
            //                sql_3.Remove(0, sql_3.Length);
            //                count = 0;
            //            }
            //        }
            //        if (sql_3.Length > 0)
            //        {

            //            db.Command(sql_3.ToString(), 360);
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        Response.Write("<script>window.alert('解密出错,请重新解密');</script>");
            //        Response.Write("<script>window.location=\"systemlogo.aspx?Projectid=" + projectid.ToString() + "&Id=1\"</script>");
            //    }


            //}
            string sql;
            string count1 = "";
            string count2 = "";

            System.Data.IDataReader dr;
            sql = " select a.cun as cun1,b.cun as cun2 from (select count(*) as cun from ProjectCompanyAttribution where BidSuccess=1 and projectid=" + projectid.ToString() + " and ProjectCompanyAttribution.accountid in (select distinct accountid from Project_Bid where projectid= " + projectid.ToString() + ")) a," +
                  " (select count(*) as cun from ProjectCompanyAttribution where BidSuccess=0 and projectid=" + projectid + " and ProjectCompanyAttribution.accountid in (select distinct accountid from Project_Bid where projectid= " + projectid.ToString() + ")) b";
            dr = db.DataReader(sql);
            while (dr.Read())
            {
                count1 = dr["cun1"].ToString();
                count2 = dr["cun2"].ToString();
            }
            dr.Close();
            dr.Dispose();
            sql = "select count(*) as cun from ProjectCompanyAttribution where memo2=1 and  projectid=" + projectid.ToString();
            dr = db.DataReader(sql);
            while (dr.Read())
            {
                if (SysFun.ToInt(dr["cun"]) > 0)
                {
                    labJM.Text = "解密已开起";
                }
                else
                {
                    labJM.Text = "解密已关闭";
                }

            }
            dr.Close();
            dr.Dispose();
            db.Close();
            TextBox1.Text = count1;
            TextBox2.Text = count2;
            showGridView();
            //}
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[3].Text == "1")
            {
                if (e.Row.Cells[6].Text == "1")
                {
                    e.Row.Cells[2].Text = "<a style='background-color:Yellow' href=PrintPrice.aspx?accountid=" + e.Row.Cells[4].Text + "&projectid=" + projectid + "&accountname=" + e.Row.Cells[0].Text + "&Usercode=" + e.Row.Cells[1].Text + " target='_blank'>已解密</a>";
                    e.Row.Cells[5].Text = "<a style='background-color:Yellow' href=PrintPrice1.aspx?accountid=" + e.Row.Cells[4].Text + "&projectid=" + projectid + "&accountname=" + e.Row.Cells[0].Text + "&Usercode=" + e.Row.Cells[1].Text + " target='_blank'>打印</a>";
                }
                else
                {
                    e.Row.Cells[2].Text = "<a href=PrintPrice.aspx?accountid=" + e.Row.Cells[4].Text + "&projectid=" + projectid + "&accountname=" + e.Row.Cells[0].Text + "&Usercode=" + e.Row.Cells[1].Text + " target='_blank'>已解密</a>";
                    e.Row.Cells[5].Text = "<a href=PrintPrice1.aspx?accountid=" + e.Row.Cells[4].Text + "&projectid=" + projectid + "&accountname=" + e.Row.Cells[0].Text + "&Usercode=" + e.Row.Cells[1].Text + " target='_blank'>打印</a>";
                }
            }
            else
            {
                e.Row.Cells[2].Text = "未解密";
                e.Row.Cells[5].Text = "";
            }
        }
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //e.Row.Cells[2].Visible = false;
        e.Row.Cells[3].Visible = false;
        e.Row.Cells[4].Visible = false;
        e.Row.Cells[6].Visible = false;

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Db db = new Db();
        string strSql = "update ProjectCompanyAttribution set memo2=1 where projectid=" + SysFun.ToTrim(Request.QueryString["projectid"]);
        db.Command(strSql);
        db.Close();
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", strSql, "../Agency/PriceDEC.aspx");

        labJM.Text = "解密已开起";

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Db db = new Db();
        string _strProjectId = SysFun.ToTrim(Request.QueryString["projectid"]);
        string strSql = "update ProjectCompanyAttribution set memo2=0 where projectid=" + _strProjectId;
        db.Command(strSql);
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", strSql, "../Agency/PriceDEC.aspx");
        string _strTable = "KaiBiaoJL" + _strProjectId;
        strSql = "if exists (select 1  from  sysobjects  where  id = object_id('" + _strTable + "')  and   type = 'U')  drop table " + _strTable;
        db.Command(strSql);
        strSql = "select ProjectId,BidId,GoodsId, ProductCode, BidTableName, BidTableType, BidTableOutlookc,ProductId, ProductName,SpellingCode,Medicinemodel, Outlookc, Unit,GoodsName, Factor, Pack,SCNAME as CompanyName_SC, AccountId_SC,FactoryUserCode as UserCode_SC,PackUnit, QualityLevels, LimitPrice,  SMPrice, SPPrice, PricePassword, RetinuePassword, AccountID_TB,TBRNAME as CompanyName_TB,TBUserCode as UserCode_TB,Convert(Varchar(255),'') as BeiZhu into " + _strTable + " from bidprice where BidSuccess=1 and CheckStatus=1 and projectid=" + _strProjectId;
        db.Command(strSql);
        strSql = "update project_bid set CheckStatus='3',memo9='报价为零废标' where (smprice=0 or smprice is null) and projectid=" + _strProjectId;  //马毅 0928  报价为 0 废标
        db.Command(strSql);
        strSql = "update project_bid set CheckStatus='3',memo9='解密未成功废标' where accountid in (select accountid  from ProjectCompanyAttribution  where projectid=" + _strProjectId + " and (BidSuccess=0 or BidSuccess is null)) And CheckStatus=1 and projectid=" + _strProjectId;
        db.Command(strSql);
        db.Close();
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "生成开标记录表", "KaiBiaoJL", strSql, "../Agency/PriceDEC.aspx");
        labJM.Text = "解密已关闭";
        Bntdel.Visible = true;
    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void showGridView()
    {
        string strConditions;
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "ProjectCompanyAttribution,company";
        dbPage.Key = "ProjectCompanyAttribution.accountid";
        dbPage.Where = strConditions;                        //
        dbPage.SelectField = "ProjectCompanyAttribution.accountid,companyname,BidSuccess,UserCode,ProjectCompanyAttribution.memo4";  //可以不设，默认为“*”


        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 30;  //Request.QueryString["PS"]);
        dbPage.OrderBy = "BidSuccess desc,ProjectCompanyAttribution.BidDate,ProjectCompanyAttribution.accountid";
        //dbPage.OrderByIsDesc = true;
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        db.Close();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }

    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        String SelDEC = DDLBidSuccess.SelectedValue.ToString();
        String BidSuccess = "";
        String strtemp = " ProjectCompanyAttribution.accountid=company.accountid and ProjectCompanyAttribution.accountid in (select distinct accountid from Project_Bid where projectid= " + projectid.ToString() + ") and" +
                         " ProjectCompanyAttribution.projectid=" + projectid.ToString() + " and"; ;
        switch (SelDEC)
        {
            case "全部":
                break;
            case "已解密成功":

                BidSuccess = "1";
                break;
            case "未解密":
                BidSuccess = "0";
                break;
        }
        //投标人注册号
        String _strUserCode_TB = txtUserCode.Text.Trim();
        if (_strUserCode_TB != "")
        {
            strtemp += " UserCode Like '%" + _strUserCode_TB + "%' And";
        }
        //投标人名称

        String _strCompanyName = txtCompanyName.Text.Trim();
        if (_strCompanyName != "")
        {
            strtemp += " companyname Like '%" + _strCompanyName + "%' And";
        }
        //解密状态

        if (BidSuccess != "")
        {
            strtemp += " BidSuccess= '" + BidSuccess + "' and";
        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }

        return strtemp;

    }
    protected void Bntdel_Click(object sender, EventArgs e)
    {
        String Sql;
        Sql = "Update Project_Bid Set CheckStatus='3' Where Accountid in (Select Accountid From ProjectCompanyAttribution Where BidSuccess=0 And Projectid=" + projectid.ToString() + ") And CheckStatus=1 And Projectid=" + projectid.ToString();
        Db db = new Db();
        db.Command(Sql);
        db.Close();
        Response.Write("<script>window.alert('废标成功！');</script>");
    }
}
